//
//  PKCarouselFigure.m
//  PianKe
//
//  Created by lanou on 15/10/2.
//  Copyright (c) 2015年 张琪. All rights reserved.
//

#import "PKCarouselFigure.h"
#import "UIImageView+WebCache.h"

@interface PKCarouselFigure () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, assign) BOOL isDragging; // 是否被拖拽
@property (nonatomic, strong) NSTimer *timer; // 定时器

@end

@implementation PKCarouselFigure

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        [self addSubview:_pageControl];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTap)];
        [self addGestureRecognizer:tap];
        
        [self addTime];
    }
    return self;
}

// 点击轮播图上面的图片响应事件
- (void)doTap
{
    if (_tapBlock) {
        self.tapBlock(self.pageControl.currentPage);
    }
}

- (void)setFigureURLArray:(NSArray *)figureURLArray
{
    _figureURLArray = figureURLArray;
    NSInteger num = _figureURLArray.count;
    
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (num + 1), self.frame.size.height);
    // 循环创建轮播图上面的imageView
    for (int i = 0; i < num; i++)
    {
        UIImageView *figureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        NSURL *url = [NSURL URLWithString:figureURLArray[i]];
        [figureImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"SX-placeholder"]];
        [self.scrollView addSubview:figureImageView];
    }
    if (num > 0)
    {
        UIImageView *figureImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * num, 0, self.frame.size.width, self.frame.size.height)];
        NSURL *url = [NSURL URLWithString:figureURLArray[0]];
        [figureImageView sd_setImageWithURL:url];
        [self.scrollView addSubview:figureImageView];
    }
    
    self.pageControl.frame = CGRectMake(self.frame.size.width - 20 * num - 10, self.frame.size.height - 30, 20 * num, 30);
    self.pageControl.numberOfPages = num;
    [self.pageControl addTarget:self action:@selector(doTapPageControl:) forControlEvents:(UIControlEventValueChanged)];
    
}

// 添加定时器
- (void)addTime
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

// 移除定时器
- (void)removeTime
{
    [self.timer invalidate];
    self.timer = nil;
}

// 定时器执行方法
- (void)scrollImage
{
    if (_isDragging) {
        return;
    }
    NSInteger page = self.pageControl.currentPage;
    page++;
    [self.scrollView setContentOffset:CGPointMake(page * self.scrollView.bounds.size.width, 0) animated:YES];
}

// 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _isDragging = YES;
    [self removeTime];
}

// 结束滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _isDragging = NO;
    [self addTime];
}

// scrollView滑动的时候pageControl变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = (scrollView.contentOffset.x + scrollView.bounds.size.width / 2) / scrollView.bounds.size.width;
    if (page == _figureURLArray.count)
    {
        self.pageControl.currentPage = 0;
        if (scrollView.contentOffset.x == _figureURLArray.count * self.scrollView.bounds.size.width)
        {
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }
    }
    else
    {
        self.pageControl.currentPage = page;
    }
}

// 点击pageControl
- (void)doTapPageControl:(UIPageControl *)pageControl
{
    [self.scrollView setContentOffset:CGPointMake(pageControl.currentPage * self.bounds.size.width, 0) animated:YES];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
